package com.ff.xyh.access.service.impl.access;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ff.xyh.access.service.access.AccessPermissionService;
import com.ff.xyh.access.service.access.AccessRolePermissionService;
import com.ff.xyh.access.service.access.AccessUserRoleService;
import com.ff.xyh.access.service.access.AccessUserService;
import com.ff.xyh.common.entity.useraccess.AccessPermission;
import com.ff.xyh.common.entity.useraccess.AccessUser;
import com.ff.xyh.common.entity.useraccess.AccessUserRole;
import com.ff.xyh.access.mapper.AccessPermissionMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 后台许可表 服务实现类
 * </p>
 *
 * @author ff
 * @since 2022-10-30
 */
@Service
public class AccessPermissionServiceImpl extends ServiceImpl<AccessPermissionMapper, AccessPermission> implements AccessPermissionService {

    @Autowired
    private AccessRolePermissionService accessRolePermissionService;
    @Autowired
    private AccessUserRoleService accessUserRoleService;

    @Autowired
    private AccessUserService accessUserService;


    @Override
    public List<String> selectPermissionValueListByUserId(String userId) {

        List<String> resultList;

        if (this.isRooter(userId)) { // root角色
//            System.out.println("is Rooter ===> ");
            resultList = this.baseMapper.selectAllPermissionValues();
        } else {
//            System.out.println("is not Rooter ===> ");
            resultList = this.baseMapper.selectPermissionValueListByUserId(userId);

        }
        if (resultList == null || resultList.isEmpty()) {
            return null;
        } else {
//            System.out.println("resultList ===> " + resultList);
            return resultList;
        }
    }


    @Override
    public List<AccessPermission> selectPermissionListByUserId(String userId) {
        List<AccessPermission> resultList = new ArrayList<>();
        List<AccessPermission> selectPermissionList;
        if(this.isRooter(userId)) { // rooter
            //如果是rooter 则全部获取

            selectPermissionList = baseMapper.selectList(null);
            AccessPermission rootPermission = selectPermissionList.stream()
                    .filter(permission -> permission.getPid().equals("0"))
                    .collect(Collectors.toList()).get(0);
            AccessPermission finalAccessPermissionObject = AccessPermission
                    .getFinalAccessPermissionObject(selectPermissionList, rootPermission);
            resultList.add(finalAccessPermissionObject);

//            List<UserBaseInfo> fromUserBaseInfoList = replyUserBaseInfos.stream().filter(
//                    replyUserBaseInfo -> replyUserBaseInfo.getId().equals(fromUserId)
//            ).collect(Collectors.toList());

        } else {
            selectPermissionList = baseMapper.selectPermissionListByUserId(userId);
            List<AccessPermission> parentPermissionList = selectPermissionList.stream()
                    .filter(permission -> permission.getPid().equals("1"))
                    .collect(Collectors.toList());
            for (AccessPermission accessPermission : parentPermissionList) {
                accessPermission = AccessPermission
                        .getFinalAccessPermissionObject(selectPermissionList, accessPermission);
                resultList.add(accessPermission);
            }
        }
        return resultList;
    }

    private boolean isRooter(String userId) {
        // 1. 获取用户角色
        QueryWrapper<AccessUserRole> accessUserRoleQueryWrapper = new QueryWrapper<>();
        accessUserRoleQueryWrapper.eq("user_id", userId);
        List<AccessUserRole> userRoleList = accessUserRoleService.list(accessUserRoleQueryWrapper);
        if (userRoleList == null || userRoleList.isEmpty()) {
            return false;
        }
        for (AccessUserRole userRole : userRoleList) {
//            System.out.println("userRole in userRoleList  ====>  " + userRole);
            if (userRole.getRoleId().equals("0")) {
                return true;
            }
        }
        return false;
    }

    /**
     * 根据用户名获取动态菜单
     * @param username
     * @return
     */
    @Override
    public List<AccessPermission> getMenu(String username) {
        AccessUser user = accessUserService.getAccessUserWithUserToken(username);

        //根据用户id获取用户菜单权限
        if (user == null) {
            return null;
        }
        List<AccessPermission> permissionList = this.selectPermissionListByUserId(user.getId());
        return permissionList;
    }
}
